package com.lhm.controller;

import com.lhm.entity.MyUser;
import com.lhm.entity.Order;
import com.lhm.service.OrderService;

import com.lhm.tool.RespJson;
import com.lhm.z.OrdInfo;
import com.lhm.z.OrderLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/fore/ord")
public class OrderController {

    //根据 getUser 拿到Token相应的登录用户的账号
    public static Object getUser(){
        Object object = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return object;
    }
    @Autowired
    OrderService orderService;
    //更新发货地址及电话
    @RequestMapping("/addAddr")
    public Map<String, Object> addAddr(@RequestBody Order ord) {
        new HashMap();
        orderService.updateOrdAddr(ord);
        return RespJson.pack(200, "添加地址成功 ......", "");
    }

    @ResponseBody
    @RequestMapping("/addPay")
    //更新付款标识
    public Map<String, Object> addPay(@RequestBody Order ord) {
        new HashMap();
        ord.setIspay(1);
        ord.setTpay(new Date());
        orderService.updateOrdPay(ord);
        Order orderById = orderService.findOrderById(ord.getOrdid());
        System.out.println(orderById);
        return RespJson.pack(200, "付款成功 ......",orderById);
    }

    //根据订单号获取该订单明细及书本信息
    @RequestMapping({"/findItemInfoByOrdid/{ordid}"})
    public Map<String, Object> findOrdItemInfo(@PathVariable("ordid") int ordid) {
        List<Map<String, Object>> lst = orderService.findItemInfoByOrdid(ordid);
        return RespJson.pack(200, "", lst);
    }

    @RequestMapping({"/findOrdLineByPage/{start}/{num}"})
    public Map<String, Object> findAllOrdLineByPages(Integer ispay, Date tstart, Date tend, @PathVariable("start") int start, @PathVariable("num") int num) {
        HashMap<String, Object> map = new HashMap();

        if (ispay != null) {
            map.put("ispay", ispay);
        }

        if (tstart != null) {
            map.put("tstart", tstart);
        }

        if (tend != null) {
            map.put("tend", tend);
        }
        MyUser mu= (MyUser) getUser();//根据myuser 拿到Token相对应的id
        //   System.out.println("========================");
        System.out.println(mu);//打印一下mu
        //如果id小于0，就没有登录，反则就登录成功，创建一个数组[CartBook]
        int muId = mu.getId();
        if (muId< 0) {
            return RespJson.pack(401, "Token过期， 请重新登录.....", null);
        } else {
            map.put("uid", muId);
            map.put("startpage", start * num);
            map.put("pagenum", num);
            System.out.println("/findOrdLineByPage/{start}/{num} 参数:" + map);
            int count = orderService.getCount(map);
            HashMap<String, Object> map1 = new HashMap();
            map1.put("total", count);

            try {
                List<OrderLine> lst = orderService.findOrdLines(map);
                map1.put("orders", lst);
                return RespJson.pack(200, "", map1);
            } catch (Exception exception) {
                exception.printStackTrace();
                return RespJson.pack(500, "查询数据有误....", map1);
            }
        }
    }

    @RequestMapping({"/findInfoByOrdid/{ordid}"})
    public Map<String, Object> findInfoByOrdid(@PathVariable("ordid") int ordid) {

        MyUser mu= (MyUser) getUser();//根据myuser 拿到Token相对应的id
        //   System.out.println("========================");
        System.out.println(mu);//打印一下mu
        //如果id小于0，就没有登录，反则就登录成功，创建一个数组[CartBook]
        int uid=mu.getId();
        if (uid < 0) {
            return RespJson.pack(401, "Token过期， 请重新登录.....", null);
        } else {
            OrdInfo obj =orderService.findInfoByOrdid(uid, ordid);
            return RespJson.pack(200, "", obj);
        }
    }
//查找该登录用户的所有订单头信息
    @RequestMapping({"/findOrdLines"})
    public Map<String, Object> findOrdLines(Integer ispay, Date tstart, Date tend) {
        HashMap<String, Object> map = new HashMap();
        if (ispay != null) {
            map.put("ispay", ispay);
        }

        if (tstart != null) {
            map.put("tstart", tstart);
        }

        if (tend != null) {
            map.put("tend", tend);
        }

        MyUser mu= (MyUser) getUser();//根据myuser 拿到Token相对应的id
        //   System.out.println("========================");
        System.out.println(mu);//打印一下mu
        //如果id小于0，就没有登录，反则就登录成功，创建一个数组[CartBook]
        int uid=mu.getId();
        if (uid < 0) {
            return RespJson.pack(401, "Token过期， 请重新登录.....", null);
        } else {
            map.put("uid", uid);
            System.out.println("/findOrdLines 参数:" + map.toString());

            try {
                List<OrderLine> lst =orderService.findOrdLines(map);
                return RespJson.pack(200, "", lst);
            } catch (Exception var7) {
                var7.printStackTrace();
                return RespJson.pack(500, "查询数据有误....", null);
            }
        }
    }

    /**
     * 自动转换日期类型的字段格式
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
